package com.rzjm.api.util;


import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

/* 2017.11.24
 * 解析pdf文件中的编号
 * */
public class getInfoFromPDF {
    private static String result = null;  // 用来保存pdf文件中的信息
    private static FileInputStream is = null;  // 输入流
    private static PDDocument document = null;
    //获取pdf文件中的编号
    public static String getAllInfoFromPDF(String pdfFilePath){
        String result = null;
        FileInputStream is = null;
        PDDocument document = null;
        try {
            is = new FileInputStream(pdfFilePath);
            RandomAccessRead randomAccessRead = new RandomAccessBufferedFileInputStream(pdfFilePath);
            PDFParser parser = new PDFParser(randomAccessRead);
            parser.parse();
            document = parser.getPDDocument();
            PDFTextStripper stripper = new PDFTextStripper();
            result = stripper.getText(document);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (document != null) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    public static void main(String[] args) throws IOException {
        /*
         * 通过递归得到某一路径下所有的目录及其PDF文件
         */
// 通过传入文件路径获取文件
        File file = new File("D:\\JavaPro\\exercise\\report");
//        String content = getPdfContent("D:\\JavaPro\\exercise\\report\\baodan.pdf");
//        System.out.println("content: " + content);
// 把获取到的文件名保存在数组中
        File[] files = file.listFiles();
        for(File f:files){
            //获取文件类型，即文件后缀名
            int start = f.getAbsolutePath().length()-3;
            int end  = f.getAbsolutePath().length();
            //得到文件的后缀名
            String pdf = f.getAbsolutePath().substring(start, end);
            //判断是否是pdf格式的文件
            if(pdf.equals("pdf") || pdf.equals("PDF")){
                // 是pdf格式的文件
                //得到全部pdf文件中的信息
                String str = getInfoFromPDF.getAllInfoFromPDF(f.getAbsolutePath());
                //截取pdf文件中的编号
                String code = str.substring(str.indexOf("：")+1,str.indexOf("：")+27);
                System.out.println("解析pdf文件后编号为:"+code);
            }
        }
    }

    /**
     * @param filePath  pdf文件路径
     * @return
     */
    public static String getPdfContent(String filePath){
        String excute = "pdftotext";

        String[] cmd = new String[]{excute, "-enc", "UTF-8", "-q", filePath,"-"};
        Process p = null;
        try {
            p = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedInputStream bis=new BufferedInputStream(p.getInputStream());
        InputStreamReader reader=null;
        try {
            reader=new InputStreamReader(bis,"UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        StringBuffer sb=new StringBuffer();

        try {
            BufferedReader br = new BufferedReader(reader);
            String line = br.readLine();
            sb = new StringBuffer();
            while (line != null) {
                sb.append(line);
                sb.append(" ");
                line = br.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}